package ua.nure.nguyen.bookstore.web.controller;

import static ua.nure.nguyen.bookstore.web.db.constant.DbConstants.DAO_FACTORY_FCN;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ua.nure.nguyen.bookstore.web.db.dao.DAO;
import ua.nure.nguyen.bookstore.web.db.dao.DAOFactory;
import ua.nure.nguyen.bookstore.web.db.entity.User;

@WebServlet("/")
public class HomeController extends HttpServlet {

	private static final long serialVersionUID = 1L;

	DAOFactory daoFactory = null;
	
	public HomeController() throws Exception {
		DAOFactory
		.setDaoFactoryFCN(DAO_FACTORY_FCN);
		daoFactory = DAOFactory.getInstance();
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			DAO<User> userDAO = daoFactory.getUserDAO();
			List<User> users = new ArrayList<User>();
			for (int i = 0; i < 10; i++) {
				users.add(userDAO.read(i));
			}
			req.setAttribute("users", users);
			req.getRequestDispatcher("/index.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
